package com.party.core.service.mall.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.core.dao.read.mall.MallShowGoodsReadDao;
import com.party.core.dao.write.mall.MallShowGoodsWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.mall.MallShowCategoryWithGoods;
import com.party.core.model.mall.MallShowGoods;
import com.party.core.model.mall.MallShowGoodsWithAuthor;
import com.party.core.model.mall.MallShowGoodsWithCategory;
import com.party.core.service.mall.IMallShowGoodsService;
import com.sun.istack.NotNull;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**展示商品实现
 * Created by wei.li
 *
 * @date 2017/8/24 0024
 * @time 15:22
 */

@Service
public class MallShowGoodsService implements IMallShowGoodsService {

    @Autowired
    private MallShowGoodsReadDao mallShowGoodsReadDao;

    @Autowired
    private MallShowGoodsWriteDao mallShowGoodsWriteDao;

    /**
     * 新增
     * @param mallShowGoods 商品
     * @return 编号
     */
    @Override
    public String insert(MallShowGoods mallShowGoods) {
        BaseModel.preInsert(mallShowGoods);
        boolean result = mallShowGoodsWriteDao.insert(mallShowGoods);
        if (result){
            return mallShowGoods.getId();
        }
        return null;
    }

    /**
     * 更新商品
     * @param mallShowGoods 商品
     * @return 更新结果（true/false）
     */
    @Override
    public boolean update(MallShowGoods mallShowGoods) {
        return mallShowGoodsWriteDao.update(mallShowGoods);
    }

    /**
     * 逻辑删除
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean deleteLogic(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)){
            return false;
        }
        return mallShowGoodsWriteDao.deleteLogic(id);
    }

    /**
     * 物理删除
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean delete(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)){
            return false;
        }
        return mallShowGoodsWriteDao.delete(id);
    }

    /**
     * 批量逻辑删除
     * @param ids 主键集合
     * @return 批量逻辑删除
     */
    @Override
    public boolean batchDeleteLogic(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return false;
        }
        return mallShowGoodsWriteDao.batchDeleteLogic(ids);
    }

    /**
     * 物理删除
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDelete(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return false;
        }
        return mallShowGoodsWriteDao.batchDelete(ids);
    }

    /**
     * 获取商品
     * @param id 主键
     * @return 商品信息
     */
    @Override
    public MallShowGoods get(String id) {
        return mallShowGoodsReadDao.get(id);
    }

    /**
     * 分页查询
     * @param mallShowGoods 商品信息
     * @param page 分页信息
     * @return 列表
     */
    @Override
    public List<MallShowGoods> listPage(MallShowGoods mallShowGoods, Page page) {
        return mallShowGoodsReadDao.listPage(mallShowGoods, page);
    }

    /**
     * 查询所有
     * @param mallShowGoods 商品信息
     * @return 列表
     */
    @Override
    public List<MallShowGoods> list(MallShowGoods mallShowGoods) {
        return mallShowGoodsReadDao.listPage(mallShowGoods, null);
    }

    /**
     * 批量查询
     * @param ids 主键集合
     * @param mallShowGoods 商品信息
     * @param page 分页信息
     * @return 列表
     */
    @Override
    public List<MallShowGoods> batchList(@NotNull Set<String> ids, MallShowGoods mallShowGoods, Page page) {
        return null;
    }

    /**
     * 商品列表
     * @param mallShowGoodsWithAuthor 商品信息
     * @param page 分页参数
     * @return 列表
     */
    @Override
    public List<MallShowGoodsWithAuthor> withAuthorListPage(MallShowGoodsWithAuthor mallShowGoodsWithAuthor, Page page) {
        return mallShowGoodsReadDao.withAuthorListPage(mallShowGoodsWithAuthor, page);
    }

    /**
     * 商品列表
     * @param mallShowCategoryWithGoods 商品
     * @param page 分页参数
     * @return 列表数据
     */
    @Override
    public List<MallShowCategoryWithGoods> withCategoryListPage(MallShowCategoryWithGoods mallShowCategoryWithGoods, Page page) {
        return mallShowGoodsReadDao.withCategoryListPage(mallShowCategoryWithGoods, page);
    }

    @Override
    public List<MallShowGoodsWithCategory> platList(String subjectId, String templateId, Page page) {
        return mallShowGoodsReadDao.platList(subjectId, templateId, page);
    }

    @Override
    public List<Map<String, Object>> getDistributorGoods(Map<String, Object> params) {
        return mallShowGoodsReadDao.getDistributorGoods(params);
    }

    @Override
    public List<MallShowGoods> findByTargetId(String targetId) {
        MallShowGoods mallShowGoods = new MallShowGoods();
        mallShowGoods.setTargetId(targetId);
        return mallShowGoodsReadDao.listPage(mallShowGoods, null);
    }

    @Override
    public void deleteBySubjectId(String subjectId) {
        mallShowGoodsWriteDao.deleteBySubjectId(subjectId);
    }

    @Override
    public List<MallShowGoods> findBySubjectId(String subjectId) {
        MallShowGoods mallShowGoods = new MallShowGoods();
        mallShowGoods.setSubjectId(subjectId);
        return mallShowGoodsReadDao.listPage(mallShowGoods, null);
    }

    @Override
    public List<MallShowGoods> findByCategoryId(String categoryId) {
        MallShowGoods mallShowGoods = new MallShowGoods();
        mallShowGoods.setCategoryId(categoryId);
        return mallShowGoodsReadDao.listPage(mallShowGoods, null);
    }
}
